package com.shazhu.api.nongjt;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.shazhu.common.annotation.Log;
import com.shazhu.common.core.controller.BaseController;
import com.shazhu.common.core.domain.AjaxResult;
import com.shazhu.common.core.page.TableDataInfo;
import com.shazhu.common.enums.BusinessType;
import com.shazhu.nongjt.domain.NjtCertificateBase;
import com.shazhu.nongjt.service.INjtCertificateBaseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 合格证基本信息Controller
 * 
 * @author shazhu
 * @date 2024-09-28
 */
@RestController
@RequestMapping("/api/certificate")
public class NjtCertificateBaseController extends BaseController
{
    @Autowired
    private INjtCertificateBaseService njtCertificateBaseService;

    /**
     * 查询合格证基本信息列表
     */
    @GetMapping("/pageList")
    public TableDataInfo list(NjtCertificateBase njtCertificateBase)
    {
        startPage();
        List<NjtCertificateBase> list = njtCertificateBaseService.selectNjtCertificateBaseList(njtCertificateBase);
        return getDataTable(list);
    }

    /**
     * 获取合格证基本信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(njtCertificateBaseService.selectNjtCertificateBaseById(id));
    }

    /**
     * 新增合格证基本信息
     */
    @Log(title = "合格证基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NjtCertificateBase njtCertificateBase)
    {
        return toAjax(njtCertificateBaseService.insertNjtCertificateBase(njtCertificateBase));
    }

    /**
     * 修改合格证基本信息
     */
    @Log(title = "合格证基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NjtCertificateBase njtCertificateBase)
    {
        return toAjax(njtCertificateBaseService.updateNjtCertificateBase(njtCertificateBase));
    }

    /**
     * 删除合格证基本信息
     */
    @Log(title = "合格证基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(njtCertificateBaseService.deleteNjtCertificateBaseByIds(ids));
    }

    /**
     * 批量获取合格证编号
     */
    @PostMapping("/batchCode")
    public AjaxResult batchGenerateNumbers(@RequestBody NjtCertificateBase njtCertificateBase)
    {
        try {
            List<String> numbers = njtCertificateBaseService.batchGenerateNumbers(njtCertificateBase);
            return AjaxResult.success(numbers);
        } catch (Exception e) {
            logger.error("批量生成合格证编号失败", e);
            return AjaxResult.error("生成合格证编号失败");
        }
    }

    /**
     * 批量保存打印记录
     */
    @PostMapping("/batchSaveByCode")
    public AjaxResult batchSavePrintRecords(@RequestBody List<String> certificateNos)
    {
        try {
            int result = njtCertificateBaseService.batchSavePrintRecords(certificateNos);
            return AjaxResult.success("保存成功", result);
        } catch (Exception e) {
            logger.error("批量保存打印记录失败", e);
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 根据ID或编号查询
     */
    @GetMapping("/queryByIdOrCode")
    public AjaxResult queryByIdOrCode(Long id, String code)
    {
        try {
            NjtCertificateBase certificate = njtCertificateBaseService.queryByIdOrCode(id, code);
            if (certificate == null) {
                return AjaxResult.error("未找到合格证信息");
            }
            return AjaxResult.success(certificate);
        } catch (Exception e) {
            logger.error("查询合格证信息失败", e);
            return AjaxResult.error("查询失败");
        }
    }

    /**
     * 开具数量统计表
     */
    @GetMapping("/countPageList")
    public TableDataInfo countList(NjtCertificateBase njtCertificateBase)
    {
        startPage();
        List<NjtCertificateBase> list = njtCertificateBaseService.selectNjtCertificateCountList(njtCertificateBase);
        return getDataTable(list);
    }
}